package com.day01.inherit;

import com.day01.basic.ArgCommand;
import com.day01.resolve.ResolveGen;
import com.day01.source.DataSource;

public abstract class DataSourceArgCommand extends ArgCommand<DataSource> {

    public DataSourceArgCommand(String type, String args) {
        super(type, ResolveGen.dataSourceResolve(args));
    }

    @Override
    public String[] keys() {
        String[] keys;
        // 尝试从redis缓存中获取
        if ((keys = readFromRedis()) != null) {
            return keys;
        }
        // redis缓存中无法获取则从数据库获取
        connect();
        keys = getKeysFromDb();
        return keys;
    }

    /**
     * 拓展连接数据库的接口
     */
    public abstract void connect();

    /**
     * 从数据库获取key
     */
    public abstract String[] getKeysFromDb();

    /**
     * 从redis缓存中获取
     */
    public String[] readFromRedis() {
        return null;
    }
}
